package src.controller;

import java.rmi.RemoteException;

import src.resource.DialogResource;
import src.resource.ValidateResource;
import src.resource.DialogResource.windowTypeEnum;
import src.service.ClientService;
import src.view.AddPostOffice;

public class AddPostOfficeController {
	
	private ClientService system;
	private AddPostOffice view;
	
	public AddPostOfficeController(ClientService system){
		this.view=AddPostOffice.getInstance(this);
		this.system=system;
		
	}
	
	public void  performSavePostOffice() throws RemoteException{
		if(!validateRequiredFields()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must complete the required fields.",windowTypeEnum.wtInformation);
			return;
		}
		if(!validateFieldsLenght()){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","The fields can not contain more than 50 characters.",windowTypeEnum.wtInformation);
			return;
		}
		
		if(system.getPostOfficeByName(view.getTxtName().getText())!=null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Name in use. Please enter another name.",windowTypeEnum.wtInformation);
			return;
		}
		
		system.savePostOffice(view.getTxtName().getText(),  null, null);
		DialogResource.showMessage(view.getBtnSave().getLocation(),"Information","Post Office saved sucessfully.",windowTypeEnum.wtConfirmation);
		this.cleanForm();
	}
	
	private void cleanForm(){
		view.getTxtName().setText("");
		
	}
	
	private boolean validateRequiredFields(){
		return ValidateResource.validateRequiredField(view.getTxtName().getText());
	}
	
	private boolean validateFieldsLenght(){
		return ValidateResource.validateStringLength(view.getTxtName().getText(), 50); 
	}

}
